//
//  MoneyCardViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/29.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "MoneyCardViewController.h"
#import "GetMoneyViewController.h"

@interface MoneyCardViewController ()<UITextFieldDelegate>

@property (nonatomic, weak)UIImageView * backImageView;

//@property (nonatomic, weak)UILabel * moneyCardLabel;
//
//@property (nonatomic, weak)UILabel * chooseBankLabel;
//
//@property (nonatomic, weak)UIImageView * chooseImage;
//
//@property (nonatomic, weak)UIView * oneLineView;

@property (nonatomic, weak)UILabel * nameLabel;

@property (nonatomic, weak)UITextField * nameField;

@property (nonatomic, weak)UIView * twoLineView;

@property (nonatomic, weak)UILabel * cardNumberLabel;

@property (nonatomic, weak)UITextField * cardNumberField;

@property (nonatomic, weak)UIView * threeLineView;

@property (nonatomic, weak)UILabel * showLabel;

@property (nonatomic, weak)UIButton * nextButton;

//@property (nonatomic, weak)UIView * fourLineView;


@end

@implementation MoneyCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"添加银行卡";
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
//    UILabel * moneyCardLabel = [[UILabel alloc] init];
//    
//    moneyCardLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
//    moneyCardLabel.textColor = ColorWithRGB(0xffffff);
//    moneyCardLabel.text = @"选择银行卡";
//    
//    self.moneyCardLabel = moneyCardLabel;
//    
//    [self.view addSubview:self.moneyCardLabel];
//    
//    
//    UILabel * chooseBankLabel = [[UILabel alloc] init];
//    
//    chooseBankLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
//    chooseBankLabel.textColor = ColorWithRGB(0x646464);
//    chooseBankLabel.text = @"请选择银行";
//    
//    self.chooseBankLabel = chooseBankLabel;
//    
//    [self.view addSubview:self.chooseBankLabel];
//    
//    
//    UIImageView * imageView = [UIImageView new];
//    
//    imageView.image = [UIImage imageNamed:@"修改头像"];
//    
//    self.chooseImage = imageView;
//    
//    [self.view addSubview:self.chooseImage];
//    
//    
//    UIView * lineOne = [UIView new];
//    
//    lineOne.backgroundColor = [UIColor whiteColor];
//    lineOne.alpha = 0.1;
//    
//    self.oneLineView = lineOne;
//    
//    [self.view addSubview:self.oneLineView];
    
    UILabel * nameLb = [UILabel new];
    
    nameLb.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    nameLb.textColor = ColorWithRGB(0xffffff);
    nameLb.text = @"户名";
    
    self.nameLabel = nameLb;
    
    [self.view addSubview:self.nameLabel];
    
    
    UITextField * moneyCardField = [[UITextField alloc] init];
    
    moneyCardField.borderStyle = UITextBorderStyleNone;
    moneyCardField.backgroundColor = [UIColor clearColor];
    moneyCardField.delegate = self;
    
    //设置光标颜色
    moneyCardField.tintColor = ColorWithRGB(0x646464);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x646464);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    moneyCardField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入用户名" attributes:attrs];
    moneyCardField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    moneyCardField.textColor = ColorWithRGB(0x646464);
    moneyCardField.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.nameField = moneyCardField;
    
    [self.view addSubview:self.nameField];
    
    
    UIView * lineTwo = [UIView new];
    
    lineTwo.backgroundColor = [UIColor whiteColor];
    lineTwo.alpha = 0.1;
    
    self.twoLineView = lineTwo;
    
    [self.view addSubview:self.twoLineView];
    
    
    UILabel * numberLb = [UILabel new];
    
    numberLb.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    numberLb.textColor = ColorWithRGB(0xffffff);
    numberLb.text = @"卡号";
    
    self.cardNumberLabel = numberLb;
    
    [self.view addSubview:self.cardNumberLabel];
    
    
    UITextField * cardNumberField = [[UITextField alloc] init];
    
    cardNumberField.borderStyle = UITextBorderStyleNone;
    cardNumberField.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    cardNumberField.tintColor = ColorWithRGB(0x646464);
    //修改占位符文字颜色

    cardNumberField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入银行卡号" attributes:attrs];
    cardNumberField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    cardNumberField.textColor = ColorWithRGB(0x646464);
    cardNumberField.delegate = self;
//    [cardNumberField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    cardNumberField.keyboardType = UIKeyboardTypeNumberPad;
    cardNumberField.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.cardNumberField = cardNumberField;
    
    [self.view addSubview:self.cardNumberField];
    
    
    UIView * lineThree = [UIView new];
    
    lineThree.backgroundColor = [UIColor whiteColor];
    lineThree.alpha = 0.1;
    
    self.threeLineView = lineThree;
    
    [self.view addSubview:self.threeLineView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11 * SCREENH6];
    showLabel.textColor = ColorWithRGB(0xf14a4a);
    showLabel.text = @"*持卡人需与账户实名认证信息一致，以免影响你的买入和提现操作";
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
    
    UIButton * nextButton = [[UIButton alloc] init];
    
    [nextButton setTitle:@"提交绑定" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17 * SCREENH6];
    
//    [nextButton setBackgroundColor:ColorWithRGB(0x979797)];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    
    nextButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    nextButton.layer.borderWidth =1.0f;
    //处理圆角
    nextButton.layer.cornerRadius =5.0f;
    [nextButton setTitleColor:ColorWithRGB(0xc9c9c9) forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(didNextButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.nextButton = nextButton;
    
    [self.view addSubview:self.nextButton];
    
    
//    UIView * lineFour = [UIView new];
//    
//    lineFour.backgroundColor = ColorWithRGB(0x575757);
    
//    self.fourLineView = lineFour;
//    
//    if (self.nextButton.isEnabled == NO) {
//        
//        self.fourLineView.hidden = NO;
//    }
//    
//    [self.view addSubview:self.fourLineView];
}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
//    [self.moneyCardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
//        make.top.mas_equalTo(self.view).offset(20 * SCREENH6);
//    }];
//    
//    [self.chooseBankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.view).offset(105 * SCREENH6);
//        make.centerY.mas_equalTo(self.moneyCardLabel);
//    }];
    
//    [self.chooseImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
//        make.centerY.mas_equalTo(self.chooseBankLabel);
//        make.width.mas_equalTo(10 * SCREENH6);
//        make.height.mas_equalTo(18 * SCREENH6);
//    }];
    
//    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(55 * SCREENH6);
//        make.height.mas_equalTo(1 * SCREENH6);
//        make.leading.trailing.mas_equalTo(self.view);
//    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15);
        make.top.mas_equalTo(self.view).offset(84 * SCREENH6);
    }];
    
    [self.nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(105 * SCREENH6);
        make.centerY.mas_equalTo(self.nameLabel);
        make.height.mas_equalTo(self.nameLabel);
        make.width.mas_equalTo(250 * SCREENH6);
    }];

    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(120 * SCREENH6);
    }];
    
    [self.cardNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(17 * SCREENH6);
        make.leading.mas_equalTo(self.nameLabel);
    }];
    
    [self.cardNumberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.nameField);
        make.centerY.mas_equalTo(self.cardNumberLabel);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(55 * SCREENH6);
    }];
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(10 * SCREENH6);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(100 * SCREENH6);
    }];
    
//    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.nextButton.mas_bottom);
//        make.leading.mas_equalTo(self.nextButton).offset(3 * SCREENH6);
//        make.trailing.mas_equalTo(self.nextButton).offset(- 3 * SCREENH6);
//        make.height.mas_equalTo(1.5 * SCREENH6);
//    }];
}


//-(void)textFieldDidChange :(UITextField *)theTextField{
//    
//    if (self.cardNumberField.text.length == 19) {
//        
//        self.nextButton.enabled = YES;
//        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
//        [self.nextButton setBackgroundColor:[UIColor clearColor]];
//        self.fourLineView.hidden = YES;
//
//    }else{
//        
//        self.nextButton.enabled = NO;
//        [self.nextButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//        [self.nextButton setBackgroundColor:ColorWithRGB(0x979797)];
//        self.fourLineView.hidden = NO;
//
//    }
//}


//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (textField == self.cardNumberField) {
//        
//        if (textField.text.length >= 19)
//            
//            textField.text = [textField.text substringToIndex:18];
//    }
//    
//    return YES;
//}


//点击提交绑定按钮
- (void)didNextButton{
    
    
    if ([self.cardNumberField.text isEqualToString:@""] || [self.nameField.text isEqualToString:@""]) {

        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善银行卡信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
//                                                                  NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSDictionary * parametersDic = @{@"bankCard":self.cardNumberField.text};
        
        [[NetworkTool sharedTools] POST:@"site/bank-card-info" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            
            if ([code isEqualToString:@"0"]) {
                
                [self boundBankCard];
                
            }else{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"服务器内部错误，请重新请求！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }];
  
        
    }
    
    
}


- (void)boundBankCard{
    
    NSDictionary * parametersDic = @{@"fullname":self.nameField.text,@"bankId":self.cardNumberField.text};
    
    [[NetworkTool sharedTools] POST:@"user/bind-bank" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你绑定银行卡成功"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];;
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
//        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"服务器内部错误，请重新请求！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.nameField resignFirstResponder];
    [self.cardNumberField resignFirstResponder];
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
